package com.hngates.project.controller;

import java.math.BigDecimal;
import java.util.List;

import com.hngates.common.utils.ShiroUtils;
import com.hngates.project.domain.ConstructionUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hngates.common.annotation.Log;
import com.hngates.common.enums.BusinessType;
import com.hngates.project.domain.ServiceContract;
import com.hngates.project.service.IServiceContractService;
import com.hngates.common.core.controller.BaseController;
import com.hngates.common.core.domain.AjaxResult;
import com.hngates.common.utils.poi.ExcelUtil;
import com.hngates.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 劳务合同Controller
 * 
 * @author ruoyi
 * @date 2022-06-24
 */
@Controller
@RequestMapping("/project/serviceContract")
public class ServiceContractController extends BaseController
{
    private String prefix = "project/serviceContract";

    @Autowired
    private IServiceContractService serviceContractService;

    @RequiresPermissions("project:serviceContract:view")
    @GetMapping()
    public String serviceContract()
    {
        return prefix + "/serviceContract";
    }

    /**
     * 查询劳务合同列表
     */
    @RequiresPermissions("project:serviceContract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ServiceContract serviceContract)
    {
        startPage();
        List<ServiceContract> list = serviceContractService.selectServiceContractList(serviceContract);
        return getDataTable(list);
    }

    /**
     * 导出劳务合同列表
     */
    @RequiresPermissions("project:serviceContract:export")
    @Log(title = "劳务合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ServiceContract serviceContract)
    {
        List<ServiceContract> list = serviceContractService.selectServiceContractList(serviceContract);
        ExcelUtil<ServiceContract> util = new ExcelUtil<ServiceContract>(ServiceContract.class);
        return util.exportExcel(list, "劳务合同数据");
    }

    /**
     * 下载模板
     *
     * @return
     */
    @RequiresPermissions("project:serviceContract:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<ServiceContract> util = new ExcelUtil<>(ServiceContract.class);
        return util.importTemplateExcel("劳务合同数据");
    }

    /**
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @RequiresPermissions("project:serviceContract:import")
    @PostMapping("/importServiceContract")
    @ResponseBody
    public AjaxResult importServiceContract(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<ServiceContract> util = new ExcelUtil<>(ServiceContract.class);
        List<ServiceContract> serviceContractList = util.importExcel(file.getInputStream(), 1);
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = serviceContractService.importServiceContract(serviceContractList, updateSupport, operName);
        return AjaxResult.success(message);
    }


    /**
     * 新增劳务合同
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存劳务合同
     */
    @RequiresPermissions("project:serviceContract:add")
    @Log(title = "劳务合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ServiceContract serviceContract)
    {
        return toAjax(serviceContractService.insertServiceContract(serviceContract));
    }

    /**
     * 修改劳务合同
     */
    @RequiresPermissions("project:serviceContract:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ServiceContract serviceContract = serviceContractService.selectServiceContractById(id);
        mmap.put("serviceContract", serviceContract);
        return prefix + "/edit";
    }

    /**
     * 修改保存劳务合同
     */
    @RequiresPermissions("project:serviceContract:edit")
    @Log(title = "劳务合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ServiceContract serviceContract)
    {
        return toAjax(serviceContractService.updateServiceContract(serviceContract));
    }

    /**
     * 删除劳务合同
     */
    @RequiresPermissions("project:serviceContract:remove")
    @Log(title = "劳务合同", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(serviceContractService.deleteServiceContractByIds(ids));
    }
}
